package com.lms.service.impl;

import com.lms.bean.Book;
import com.lms.bean.Type;
import com.lms.dao.BookDao;
import com.lms.dao.TypeDao;
import com.lms.service.TypeService;
import com.lms.util.LogUtils;
import com.lms.util.MyStringUtils;
import com.lms.vo.TypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeDao typeDao;

    @Autowired
    private BookDao bookDao;

    @Override
    public Long getTotalCount() {
        return typeDao.count();
    }

    @Override
    public List<TypeVO> findAll(int page, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.ASC, "typeId");
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        List<Type> list = typeDao.getList(pageable);
        List<TypeVO> typeVOList = new ArrayList<>();
        list.forEach(type -> typeVOList.add(TypeVO.convert(type)));
        return typeVOList;
    }

    @Override
    public Long getTotalCount(String queryCondition) {
        String s = StringUtils.trimAllWhitespace(queryCondition);
        if (MyStringUtils.isNumeric(s)) {
            return typeDao.countAllByTypeIdOrTypenameLike(Long.parseLong(s), "%" + s + "%");
        } else {
            return typeDao.countAllByTypenameLike("%" + s + "%");
        }
    }

    @Override
    public List<TypeVO> findAll(int page, int pageSize, String queryCondition) {
        Sort sort = Sort.by(Sort.Direction.ASC, "typeId");
        Pageable pageable = PageRequest.of(page, pageSize, sort);

        List<Type> list;
        String s = StringUtils.trimAllWhitespace(queryCondition);
        if (MyStringUtils.isNumeric(s)) {
            list = typeDao.findAllByTypeIdOrTypenameLikePage(Long.parseLong(s), "%" + s + "%",pageable);
        } else {
            list = typeDao.findAllByTypenameLikePage("%" + s + "%",pageable);
        }
        List<TypeVO> typeVOList = new ArrayList<>();
        list.forEach(type -> typeVOList.add(TypeVO.convert(type)));
        return typeVOList;
    }

    @Override
    public Type findTypeById(Long typeId) {
        return typeDao.getOne(typeId);
    }

    @Override
    public void save(Type type) {
        typeDao.save(type);
    }

    @Override
    public void deleteById(Long typeId) {
        if (bookDao.findAllByTypeTypeId(typeId).size() == 0) {
            typeDao.deleteById(typeId);
        }
    }

    @Override
    @Transactional
    public void deleteAll(String ids) {
        String[] split = ids.split(",");
        List<Long> Ids = new ArrayList<>();
        for (String s : split) {
            if (MyStringUtils.isNumeric(s)) {
                Ids.add(Long.parseLong(s));
            }
        }
        for (Long id : Ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Type> findAll() {
        return typeDao.findAll();
    }
}
